package table

import (
	"fmt"
	"i18n-template/components"
	"net/url"
)

type Inductance struct {
	CommentHeader
	InstallationMethod   string `json:"InstallationMethod" gorm:"type:text;column:InstallationMethod;comment:安装方式"`
	RatedDistanceSn      string `json:"RatedDistanceSn" gorm:"type:text;column:RatedDistanceSn;comment:额定距离 Sn"`
	SafeDistanceSa       string `json:"SafeDistanceSa" gorm:"type:text;column:SafeDistanceSa;comment:确保距离 Sa"`
	Dimension            string `json:"Dimension" gorm:"type:text;column:Dimension;comment:外形规格"`
	OutputType           string `json:"OutputType" gorm:"type:text;column:OutputType;comment:输出方式"`
	PowerSupplyVoltage   string `json:"PowerSupplyVoltage" gorm:"type:text;column:PowerSupplyVoltage;comment:电源电压"`
	StandardTarget       string `json:"StandardTarget" gorm:"type:text;column:StandardTarget;comment:标准标靶"`
	SwitchPointDeviation string `json:"SwitchPointDeviation" gorm:"type:text;column:SwitchPointDeviation;comment:开关点偏移[%/Sr]"`
	HysteresisRange      string `json:"HysteresisRange" gorm:"type:text;column:HysteresisRange;comment:迟滞范围[%/Sr]"`
	RepeatError          string `json:"RepeatError" gorm:"type:text;column:RepeatError;comment:重复误差"`
	LoadCurrent          string `json:"LoadCurrent" gorm:"type:text;column:LoadCurrent;comment:负荷电流"`
	ResidualVoltage      string `json:"ResidualVoltage" gorm:"type:text;column:ResidualVoltage;comment:残留电压"`
	LeakageCurrent       string `json:"LeakageCurrent" gorm:"type:text;column:LeakageCurrent;comment:漏电流"`
	ProtectionCircuit    string `json:"ProtectionCircuit" gorm:"type:text;column:ProtectionCircuit;comment:保护回路"`
	PowerIndicator       string `json:"PowerIndicator" gorm:"type:text;column:PowerIndicator;comment:电源指示"`
	AmbientTemperature   string `json:"AmbientTemperature" gorm:"type:text;column:AmbientTemperature;comment:环境温度"`
	AmbientHumidity      string `json:"AmbientHumidity" gorm:"type:text;column:AmbientHumidity;comment:环境湿度"`

	SwitchingFrequency      string `json:"SwitchingFrequency" gorm:"type:text;column:SwitchingFrequency;comment:开关频率"`
	ImpulseWithstandVoltage string `json:"ImpulseWithstandVoltage" gorm:"type:text;column:ImpulseWithstandVoltage;comment:冲击耐压"`
	InsulationResistance    string `json:"InsulationResistance" gorm:"type:text;column:InsulationResistance;comment:绝缘阻抗"`
	VibrationResistance     string `json:"VibrationResistance" gorm:"type:text;column:VibrationResistance;comment:耐振动"`
	ProtectionLevel         string `json:"ProtectionLevel" gorm:"type:text;column:ProtectionLevel;comment:防护等级"`
	ShellMaterial           string `json:"ShellMaterial" gorm:"type:text;column:ShellMaterial;comment:外壳材料"`
	WiringMethod            string `json:"WiringMethod" gorm:"type:text;column:WiringMethod;comment:接线方式"`
	OutputMethod            string `json:"OutputMethod" gorm:"type:text;column:OutputMethod;comment:输出方式"`
	OutputCurrent           string `json:"OutputCurrent" gorm:"type:text;column:OutputCurrent;comment:输出电流"`
	PressureDrop            string `json:"PressureDrop" gorm:"type:text;column:PressureDrop;comment:压降"`
	LoadImpedance           string `json:"LoadImpedance" gorm:"type:text;column:LoadImpedance;comment:负载阻抗"`
	InsulationImpedance     string `json:"InsulationImpedance" gorm:"type:text;column:InsulationImpedance;comment:绝缘阻抗"`
	ConnectionMethod        string `json:"ConnectionMethod" gorm:"type:text;column:ConnectionMethod;comment:接线方式"`

	DetectionDistance  string `json:"DetectionDistance" gorm:"type:text;column:DetectionDistance;comment:检测距离"`
	Linearity          string `json:"Linearity" gorm:"type:text;column:Linearity;comment:线性度"`
	ConsumptionCurrent string `json:"ConsumptionCurrent" gorm:"type:text;column:ConsumptionCurrent;comment:消耗电流"`
}

func (in Inductance) TableName() string {
	return "inductance"
}

func (in Inductance) GenerateInductive(i *Inductance) error {
	return components.DB.Model(&Inductance{}).Save(i).Error
}

func (in Inductance) FindInductiveByMainAttribute(path url.Values) ([]*Inductance, error) {
	ind := make([]*Inductance, 100)
	str := ""
	for key, value := range path {
		fmt.Println("=> key,value", key, value)
		if key == "Name" {
			if str == "" && value[0] != "" {
				str = fmt.Sprintf("%s LIKE '%s'", key, "%"+value[0]+"%")
			} else if str != "" && value[0] != "" {
				str += fmt.Sprintf(" AND %s LIKE '%s'", key, "%"+value[0]+"%")
			}
		} else {
			if str == "" && value[0] != "" {
				str = fmt.Sprintf("%s = '%s'", key, value[0])
			} else if str != "" && value[0] != "" {
				str += fmt.Sprintf(" AND %s = '%s'", key, value[0])
			}
		}

	}
	fmt.Println("=> end url ", str)
	err := components.DB.Model(&Inductance{}).Where(str).Find(&ind).Error
	return ind, err
}

func (in Inductance) DeleteInductiveByID(i *Inductance) error {
	return components.DB.Model(&i).
		Where("id = ?", i.ID).
		First(&i).
		Delete(&i).Error
}

func (in Inductance) UpdateInductiveByID(i *Inductance) error {
	return components.DB.Model(&i).Where("id = ?", i.ID).First(&Inductance{}).Updates(i).Error
}
